package com.dreamthen.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author zengms
 * @Date 2023-11-05 16:08:41
 **/
@Controller
@RequestMapping("/data")
@SessionAttributes({"sessionType","type"})
public class DataController {

    /**
     * 通过servlet原生api返回数据给页面
     * @return
     */
    @GetMapping("/servletApi")
    public String servletApi(HttpServletRequest request){
        request.setAttribute("type","servlet-Api");
        return "main";
    }


    /**
     * 通过Model方式传参给页面
     * @param model
     * @return
     */
    @GetMapping("/model")
    public String model(Model model){
        model.addAttribute("type","model");
        model.addAttribute("sessionType","session-type");
        return "main";
    }


    /**
     * 通过Map方式传参给页面
     * @param map
     * @return
     */
    @GetMapping("/map")
    public String map(Map map){
        map.put("type","map");
        return "main";
    }

    /**
     * 通过ModelMap方式传参给页面
     * @param modelMap
     * @return
     */
    @GetMapping("/modelMap")
    public String modelMap(ModelMap modelMap){
        modelMap.addAttribute("type","modelMap");
        return "main";
    }


    /**
     * 通过ModelAndView 传输数据给页面
     * @return
     */
    @GetMapping("/mv")
    public ModelAndView mv(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("type","mv");
        mv.setViewName("main");
        return mv;
    }


}
